const express = require("express");
const router = express.Router();
const userServ = require("../../services/userService");
const recordServ = require("../../services/recordService");
const { asyncHandler } = require("../getSendResult");

const { pick } = require("../../util/propertyHelper");

router.post("/add", asyncHandler(async (req, res) => {
  const { id } = req.user;

  await recordServ.add({ ...req.body, UserId: id });

  return { data: '' };

}));

router.get("/", asyncHandler(async (req, res) => {

  const { id } = req.user;
  const page = req.query.page || 1;
  const limit = req.query.limit || 10;
  const date = req.query.date || '';
  const records = await recordServ.getList(id, page, limit, date);

  return { data: records };
}));

router.put("/:id", asyncHandler(async (req, res) => {
  const { id } = req.user;
  const recordId = req.params.id;

  await recordServ.update(recordId, { ...req.body, UserId: id });

  return { data: '' };
}))

router.delete("/:id", asyncHandler(async (req, res) => {

  const { id } = req.user;
  const recordId = req.params.id;

  await recordServ.delete(id, recordId);

  return { data: '' };
}))

module.exports = router;